package msr.service;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.logging.Logger;

import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;

import com.opensymphony.xwork2.ActionContext;

import msr.object.User;
import msr.util.ConnectDb;
import msr.util.QueryDefine;

/**
 * ユーザに関することにログイン、ログアウト、パスワード変更という各操作がこにに実施される。
 * */
public class UserService {

	private static final Logger log = Logger.getLogger(OracleService.class
			.getName());

	private static ConnectDb connectdb = ConnectDb.getInstance();

	/**
	 * パスワード変更機能
	 * 
	 * @param newPasswd
	 *            : 新しいパスワード
	 * @return true if success
	 * */
	public boolean changePasswd(String newPasswd) {
		if (newPasswd != null) {
			// get all product when show list
			Connection conn = connectdb.connectDatabase();
			if (conn != null) {
				try {
					String sql = QueryDefine.QUERY_CHANGE_PASSWD;
					Map<String, Object> session = ActionContext.getContext()
							.getSession();
					CallableStatement cstmt = conn.prepareCall(sql);
					cstmt.setString(1, (String) session.get("username"));
					cstmt.setString(2, newPasswd);
					int result = cstmt.executeUpdate();
					if (result > 0)
						return true;
					else
						return false;
				} catch (SQLException ex) {
					log.info("sqlexception " + ex.getMessage());
					ex.printStackTrace();
				}
			} else
				return false;
		} else
			return false;
		return true;
	}

	/**
	 * 
	 * check passwd by username from oracle database
	 * 
	 * @return true if success ,otherwise is false
	 * */
	public boolean checkLogin(String username, String passwd) {
		Connection conn = connectdb.connectDatabase();
		if (conn != null) {
			try {
				String sql = QueryDefine.QUERY_USER;
				CallableStatement vStatement = conn.prepareCall(sql);
				vStatement.registerOutParameter(1, OracleTypes.CURSOR);
				vStatement.setString(2, username);
				vStatement.setString(3, passwd);
				vStatement.execute();
				ResultSet rs = ((OracleCallableStatement) vStatement)
						.getCursor(1);
				if (!rs.next()) {
					log.info("result set is null");
					return false;
				}
			} catch (SQLException ex) {
				log.info("sqlexception " + ex.getMessage());
				ex.printStackTrace();
				return false;
			}
		}
		return true;
	}

	/**
	 * 
	 * get user from database
	 * 
	 * @return true if success ,otherwise is false
	 * */
	public User checkLoginReturnUser(String username, String passwd) {
		Connection conn = connectdb.connectDatabase();
		if (conn != null) {
			try {
				// need instead by PLSQL
				String sql = QueryDefine.QUERY_USER;
				CallableStatement vStatement = conn.prepareCall(sql);
				vStatement.registerOutParameter(1, OracleTypes.CURSOR);
				vStatement.setString(2, username);
				vStatement.setString(3, passwd);
				vStatement.execute();
				ResultSet re = ((OracleCallableStatement) vStatement)
						.getCursor(1);
				if (!re.next()) {
					log.info("result set is null");
					return null;
				} else {
					User u = new User(re.getString("WEB_USER"),
							re.getString("PASSWORD"), re.getString("PREF_CD"),
							re.getString("FARM_CD"), re.getString("ACOP_CD"));
					return u;
				}

			} catch (SQLException ex) {
				log.info("sqlexception " + ex.getMessage());
				ex.printStackTrace();
				return null;
			}
		}
		return null;
	}

	/**
	 * test check login
	 * */
	public User testCheckLogin(String username, String passwd) {
		User u = new User();
		if (username.equals("admin") && passwd.equals("passwd")) {
			u.setUsername(username);
			u.setPasswd(passwd);
			return u;
		}
		return null;
	}

	public User getLoginUser(String username) {
		Connection conn = connectdb.connectDatabase();
		if (username != null)
			if (conn != null)
				try {
					String sql = QueryDefine.QUERY_USER_BY_ID;
					CallableStatement vStatement = conn.prepareCall(sql);
					vStatement.registerOutParameter(1, OracleTypes.CURSOR);
					vStatement.setString(2, username);
					vStatement.execute();
					ResultSet re = ((OracleCallableStatement) vStatement)
							.getCursor(1);
					if (!re.next()) {
						log.info("result set is null");
						return null;
					} else {
						User u = new User(re.getString("WEB_USER"),
								re.getString("PASSWORD"),
								re.getString("PREF_CD"),
								re.getString("FARM_CD"),
								re.getString("ACOP_CD"));
						return u;
					}
				} catch (SQLException ex) {
					log.info("sqlexception " + ex.getMessage());
					ex.printStackTrace();
					return null;
				}
		return null;
	}

	/**
	 * function get up_user by username
	 * 
	 * @param username
	 *            : login user name
	 * @return upuser name
	 * */
	public String getUpUserByUsername(String username) {
		String upuser = null;
		if (username != null) {
			Connection conn = connectdb.connectDatabase();
			if (conn != null) {
				try {
					String query = QueryDefine.QUERY_GET_UPUSER;
					log.info("valeu of query " + query);
					CallableStatement vStatement = conn.prepareCall(query);
					vStatement.registerOutParameter(1, OracleTypes.CURSOR);
					vStatement.setString(2, username);
					vStatement.execute();
					ResultSet rs = ((OracleCallableStatement) vStatement)
							.getCursor(1);
					while (rs.next()) {
						upuser = rs.getString("UP_USER");
					}
				} catch (SQLException ex) {
					log.info("sqlexception " + ex.getMessage());
					ex.printStackTrace();
				}
			} else
				return null;
		} else
			return null;
		return upuser;
	}

	/**
	 * check current password wherer equal new passwowrd or new password is
	 * equal confirm password
	 * */
	public boolean checkEqualPassword(String passwd1, String passwd2) {
		if (passwd1.equals(passwd2))
			return true;
		else
			return false;
	}
}
